<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Goods as GoodsModel;
use app\admin\model\Type;
use think\Validate;
class Goods extends Controller
{
	private  $goodsModel;
    public function _initialize() {
        $this->goodsModel = new GoodsModel();
    }
	public function index()
	{
		$count = $this->goodsModel->where(['dels'=>1])->count();
		$goods = $this->goodsModel->getGoods();
		$types = model('Type')->getNormalFirstType();
		return $this->fetch('',[
			'goods' => $goods,
			'types' => $types,
			'count' => $count,
		]);
	}

	public function insert()
	{
		$types = model('Type')->getNormalFirstType();
		return $this->fetch('',['types' => $types]);
	}
	// 商品详情
	public function details()
	{
		$id = input('get.id');
		$info = $this->goodsModel->get($id);
		$firstId = $info->category1_id;
		$secondId = $info->category2_id;
		$types = model('Type')->getNormalFirstType();
		$secType = model('Type')->getFirstTypes($firstId);
		return $this->fetch('',[
			'info' => $info,
			'types' => $types,
			'secType' => $secType,
			'firstId' => $firstId,
			'secondId' => $secondId,
		]);
	}

	// 添加商品
	public function add()
	{
		$data = input('post.');
		$file = request()->file('image');
		$info = $file->validate(['size'=>2000000,'exe'=>'jpg,png,gif'])->rule('uniqid')->move(ROOT_PATH.'public'.DS.'upload');
		if ($info) {
			unset($data['image']);
			$data['status'] = 1;
			$data['picture'] = '/upload/'.$info->getFilename();
			$data['hot'] = 5;
			$res = $this->goodsModel->save($data);
			if ($res) {
				$this->success('添加成功');
			} else {
				$this->error('添加失败');
			}
		} else {
			echo $file->getError();
		}
	}

	//修改价格
	public function changeprice($id,$price)
	{
		if (!request()->isPost()) {
			$this->error('提交不合法');
		}
		$data = [
			'id' => $id,
			'price' => $price,
		];
		$validate = Validate('Goods');
		if (!$validate->scene('changeprice')->check($data)) {
			$this->error($validate->getError());
		}
		$res = $this->goodsModel->save(['price'=>$price],['id'=>$id]);
		if ($res) {
			$this->result($_SERVER['HTTP_REFERER'], 1, 'success');
		} else {
			$this->result($_SERVER['HTTP_REFERER'], 0, '更新失败');
		}
	}

	//修改库存
	public function changestock($id,$stock)
	{
		if (!request()->isPost()) {
			$this->error('提交不合法');
		}
		$data = [
			'id' => $id,
			'stock' => $stock,
		];
		$validate = Validate('Goods');
		if (!$validate->scene('changestock')->check($data)) {
			$this->error($validate->getError());
		}
		$res = $this->goodsModel->save(['stock'=>$stock],['id'=>$id]);
		if ($res) {
			$this->result($_SERVER['HTTP_REFERER'], 1, 'success');
		} else {
			$this->result($_SERVER['HTTP_REFERER'], 0, '更新失败');
		}
	}

	public function save()
	{
		if (!request()->isPost()) {
			$this->error('提交不合法');
		}
		$data = input('post.');
		$id = $data['id'];
		unset($data['id']);
		$res = $this->goodsModel->save($data,['id'=>$id]);
		if ($res) {
			$this->success('保存成功');
		} else {
			$this->error('保存失败');
		}
	}

	public function delete()
	{
		if (!request()->isGet()) {
			$this->error('请求不合法');
		}
		$data = input('get.');
		$validate = validate('Goods');
		if (!$validate->scene('delete')->check($data)) {
			$this->error($validate->getError());
		}
		$res = $this->goodsModel->save(['dels'=>$data['dels']],['id'=>$data['id']]);
		if ($res) {
			$this->success('商品删除成功');
		} else {
			$this->error('商品删除失败');
		}
	}
}